package com.jet.chat.command;
import java.util.ArrayList;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.jet.chat.server.ChatServer;
import com.jet.chat.server.ChatSession;
import com.jet.chat.service.ChatMessage;
import com.jet.chat.service.ChatMessageService;


public class ChatCommandViewChannelHistory implements ChatCommand {

	@Override
	public void execute(ChatServer chatServer, ChatSession chatSession, ChatMessage chatMessage) {
		Pattern pattern = Pattern.compile("^/\\w+$");
		Matcher matcher = pattern.matcher(chatMessage.getMessage());
		if (!matcher.matches()) {
			ChatMessageService.send(new ChatMessage("invalid command format"), chatSession.getWriter());
		} else {
			String responce = "";
			ArrayList<Properties> history = chatServer.getDbService().getChannelHistory(chatSession.getChannelName());
			for (Properties raw : history) {
				responce = responce + raw.getProperty("username") + " > " + raw.getProperty("content") + "\r\n";
			}
			ChatMessageService.send(new ChatMessage(responce), chatSession.getWriter());
		}
	}

}
